MariaDB [class]> select cid,cname,ch,ebg,math,ch+ebg+math sum from bk1;
+-----+--------+------+-----+------+------+
| cid | cname | ch | ebg | math | sum |
+-----+--------+------+-----+------+------+
| 01 | 簡奉君 | 13 | 59 | 0 | 72 |
| 02 | 黃靖輪 | 100 | 32 | 100 | 232 |
| 03 | 潘四敬 | 5 | 48 | 100 | 153 |
| 04 | 賴勝恩 | 63 | 52 | 100 | 215 |
| 05 | 黎楚寧 | 4 | 6 | 100 | 110 |
| 06 | 蔡中穎 | 70 | 95 | 100 | 265 |
| 07 | 徐佳螢 | 28 | 56 | 100 | 184 |
| 08 | 林雨媗 | 96 | 0 | 100 | 196 |
| 09 | 林心儀 | 79 | 45 | 100 | 224 |
| 10 | 王燕博 | 15 | 4 | 100 | 119 |
| 11 | Brad | 99 | 88 | 33 | 220 |
+-----+--------+------+-----+------+------+
11 rows in set (0.059 sec)
MariaDB [class]> create view scoreview as
-> select cid,cname,ch,ebg,math,ch+ebg+math sum from bk1;
Query OK, 0 rows affected (0.019 sec)
MariaDB [class]> select * from scoreview;
+-----+--------+------+-----+------+------+
| cid | cname | ch | ebg | math | sum |
+-----+--------+------+-----+------+------+
| 01 | 簡奉君 | 13 | 59 | 0 | 72 |
| 02 | 黃靖輪 | 100 | 32 | 100 | 232 |
| 03 | 潘四敬 | 5 | 48 | 100 | 153 |
| 04 | 賴勝恩 | 63 | 52 | 100 | 215 |
| 05 | 黎楚寧 | 4 | 6 | 100 | 110 |
| 06 | 蔡中穎 | 70 | 95 | 100 | 265 |
| 07 | 徐佳螢 | 28 | 56 | 100 | 184 |
| 08 | 林雨媗 | 96 | 0 | 100 | 196 |
| 09 | 林心儀 | 79 | 45 | 100 | 224 |
| 10 | 王燕博 | 15 | 4 | 100 | 119 |
| 11 | Brad | 99 | 88 | 33 | 220 |
+-----+--------+------+-----+------+------+
11 rows in set (0.001 sec)
MariaDB [class]> update bk1 set math=100 where cid=1;
Query OK, 1 row affected (0.051 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//查看scoreview是否也修改?
//查看後,資料也被修改了
MariaDB [class]> select * from scoreview;
+-----+--------+------+-----+------+------+
| cid | cname | ch | ebg | math | sum |
+-----+--------+------+-----+------+------+
| 01 | 簡奉君 | 13 | 59 | 100 | 172 |
| 02 | 黃靖輪 | 100 | 32 | 100 | 232 |
| 03 | 潘四敬 | 5 | 48 | 100 | 153 |
| 04 | 賴勝恩 | 63 | 52 | 100 | 215 |
| 05 | 黎楚寧 | 4 | 6 | 100 | 110 |
| 06 | 蔡中穎 | 70 | 95 | 100 | 265 |
| 07 | 徐佳螢 | 28 | 56 | 100 | 184 |
| 08 | 林雨媗 | 96 | 0 | 100 | 196 |
| 09 | 林心儀 | 79 | 45 | 100 | 224 |
| 10 | 王燕博 | 15 | 4 | 100 | 119 |
| 11 | Brad | 99 | 88 | 33 | 220 |
+-----+--------+------+-----+------+------+
11 rows in set (0.001 sec)
MariaDB [class]> update scoreview set math=0 where cid=1;
MariaDB [class]> select * from bk1\G;
*************************** 1. row ***************************
cID: 01
cName: 簡奉君
cSex: F
cBirthday: 1987-04-04
cEmail: elven@superstar.com
cPhone: 0922988876
cAddr: 台北市濟洲北路12號
ch: 13
ebg: 59
math: 0
*************************** 2. row ***************************
cID: 02
cName: 黃靖輪
cSex: M
cBirthday: 1987-07-01
cEmail: jinglun@superstar.com
cPhone: 0918181111
cAddr: 台北市敦化南路93號5樓
ch: 100
ebg: 32
math: 100
*************************** 3. row ***************************
.......省略
註:一般不會從view去修改值
MariaDB [class]> insert into bk1 values (11,'Brad','M','1999-01-02','','','',10,20,30);
Query OK, 1 row affected (0.013 sec)
MariaDB [class]> select * from scoreview;
+-----+--------+------+-----+------+------+
| cid | cname | ch | ebg | math | sum |
+-----+--------+------+-----+------+------+
| 01 | 簡奉君 | 13 | 59 | 0 | 72 |
| 02 | 黃靖輪 | 100 | 32 | 100 | 232 |
| 03 | 潘四敬 | 5 | 48 | 100 | 153 |
| 04 | 賴勝恩 | 63 | 52 | 100 | 215 |
| 05 | 黎楚寧 | 4 | 6 | 100 | 110 |
| 06 | 蔡中穎 | 70 | 95 | 100 | 265 |
| 07 | 徐佳螢 | 28 | 56 | 100 | 184 |
| 08 | 林雨媗 | 96 | 0 | 100 | 196 |
| 09 | 林心儀 | 79 | 45 | 100 | 224 |
| 10 | 王燕博 | 15 | 4 | 100 | 119 |
| 11 | Brad | 10 | 20 | 30 | 60 |
+-----+--------+------+-----+------+------+
12 rows in set (0.001 sec)
MariaDB [class]> inster into scoreview values (12,'tony',1,2,3);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'inster into scoreview values (12,'tony',1,2,3)' at line 1
MariaDB [class]>
清空裡面所有tables
分別在重新建立tb1,tb2兩個資料表
MariaDB [db1]> create table tb2 (id int,f2 varchar(10), tbf1 varchar(10));
MariaDB [db1]> desc tb2;
+------- +-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----- --+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| f2 | varchar(10) | YES | | NULL | |
| tb1f1 | varchar(10) | YES | | NULL | |
+---- ---+-------------+------+-----+---------+-------+
3 rows in set (0.022 sec)
MariaDB [db1]> insert into tb1 values (1,'A'),(2,'B'),(3,'C'),(4,'D');
Query OK, 4 rows affected (0.050 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [db1]> select * from tb1;
+------+------+
| id | f1 |
+------+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
+------+------+
4 rows in set (0.000 sec)
MariaDB [db1]> insert into tb2 value (1,'AA','A');
Query OK, 1 row affected (0.050 sec)
MariaDB [db1]> insert into tb2 value (2,'BB','B');
Query OK, 1 row affected (0.051 sec)
MariaDB [db1]> insert into tb2 value (3,'CC','Z');
Query OK, 1 row affected (0.050 sec)
MariaDB [db1]> select * from tb2;
+------+------+------+
| id | f2 | tbf1 |
+------+------+------+
| 1 | AA | A |
| 2 | BB | B |
| 3 | CC | Z |
+------+------+------+
MariaDB [db1]> select tb1.id,tb1.f1,tb2.f2 from tb1
-> join tb2 on (tb1.f1 = tb2.tbf1);
+------+------+------+
| id | f1 | f2 |
+------+------+------+
| 1 | A | AA |
| 2 | B | BB |
+------+------+------+
2 rows in set (0.001 sec)
MariaDB [db1]> select tb1.id,tb1.f1,tb2.f2 from tb1
-> left join tb2 on (tb1.f1 = tb2.tbf1);
+------+------+------+
| id | f1 | f2 |
+------+------+------+
| 1 | A | AA |
| 2 | B | BB |
| 3 | C | NULL |
| 4 | D | NULL |
+------+------+------+
4 rows in set (0.001 sec)
MariaDB [db1]> select tb1.id,tb1.f1,tb2.f2 from tb1
-> right join tb2 on (tb1.f1 = tb2.tbf1);
+------+------+------+
| id | f1 | f2 |
+------+------+------+
| 1 | A | AA |
| 2 | B | BB |
| NULL | NULL | CC |
+------+------+------+
3 rows in set (0.000 sec)
MariaDB [db1]> select tb1.id,tb1.f1,tb2.f2 from tb1
-> join tb2 on (tb1.f1 = tb2.tbf1);
+------+------+------+
| id | f1 | f2 |
+------+------+------+
| 1 | A | AA |
| 2 | B | BB |
+------+------+------+
2 rows in set (0.001 sec)
MariaDB [db1]> create view myview as
-> select tb1.id,tb1.f1,tb2.f2 from tb1
-> join tb2 on (tb1.f1 = tb2.tbf1);
Query OK, 0 rows affected (0.020 sec)
MariaDB [db1]> show tables;//資料庫中多了一個view
+---------------+
| Tables_in_db1 |
+---------------+
| myview |
| tb1 |
| tb2 |
+---------------+
3 rows in set (0.001 sec)
MariaDB [db1]> desc myview;//此view的欄位
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| f1 | varchar(10) | YES | | NULL | |
| f2 | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.024 sec)
MariaDB [db1]> select * from myview;//view內容同上述的1.
+------+------+------+
| id | f1 | f2 |
+------+------+------+
| 1 | A | AA |
| 2 | B | BB |
+------+------+------+
2 rows in set (0.001 sec)
MariaDB [db1]> update tb2 set tbf1 ='C' where id=3;
Query OK, 1 row affected (0.014 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [db1]> select * from tb2;
+------+------+------+
| id | f2 | tbf1 |
+------+------+------+
| 1 | AA | A |
| 2 | BB | B |
| 3 | CC | C |
+------+------+------+
3 rows in set (0.000 sec)
MariaDB [db1]> select * from myview;
+------+------+------+
| id | f1 | f2 |
+------+------+------+
| 1 | A | AA |
| 2 | B | BB |
| 3 | C | CC |
+------+------+------+
3 rows in set (0.000 sec)